﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Common;
using System.Collections.ObjectModel;

namespace AnesSystem.Controls
{
    /// <summary>
    /// ClassNameCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ClassNameCtrl : UserControl
    {
        #region Variables

        #endregion

        #region Constructor
        public ClassNameCtrl()
        {
            InitializeComponent();
            GetClasses();
        }


        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(ClassNameCtrl), new PropertyMetadata(""));




        public ObservableCollection<string> DisplayList
        {
            get { return (ObservableCollection<string>)GetValue(DisplayListProperty); }
            set { SetValue(DisplayListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DisplayList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DisplayListProperty =
            DependencyProperty.Register("DisplayList", typeof(ObservableCollection<string>), typeof(ClassNameCtrl), new PropertyMetadata(new ObservableCollection<string>()));



        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void GetClasses()
        {
            DisplayList.Clear();
            foreach (var item in DataManager.Instance.ClassList)
            {
                DisplayList.Add(item);
            }
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void _txtName_GotFocus(object sender, RoutedEventArgs e)
        {
            _popup.IsOpen = true;
        }

        private void _txtName_LostFocus(object sender, RoutedEventArgs e)
        {
            _popup.IsOpen = false;
        }

        private void _txtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            DisplayList.Clear();
            foreach (var item in DataManager.Instance.ClassList)
            {
                if (item.IsContain(_txtName.Text))
                {
                    DisplayList.Add(item);
                }
            }
        }

        private void _listCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _txtName.TextChanged -= _txtName_TextChanged;
            Value = _listCtrl.SelectedValue.ToString();
            _txtName.TextChanged += _txtName_TextChanged;
        }

        #endregion


    }
}
